<?php
namespace app\api\controller;
use think\Db;
use think\Request;
use think\Session;
use think\Controller;
class Zhuye extends Controller
{
    public function AjaxReturn($code,$msg,$data=array())
    {
        $returnData=array(
            "code"=>$code
        );
        if($msg)
        {
            $returnData["message"]=$msg;
        }
        if(!empty($data))
        {
            $returnData["data"]=$data;
        }
        exit(json_encode($returnData));
    }
    //主页面显示(已测试)
    public function index()
    {
        $code=input('openid');
        $uid=Db::name('user')->where('code',$code)->field('uid')->find();
        $gid=input('uid');
        if($gid=='')
        {
            $arr = Db::name('user')
                ->join('f_company','f_company.uid=f_user.uid')
                ->join('f_real','f_real.uid=f_user.uid')
                ->join('f_fans','f_fans.uid=f_user.uid')
                ->where('f_user.uid',$uid['uid'])
                ->field('f_user.nickname,f_user.uname,f_user.avatars_img,f_user.company,f_user.head_portrait,f_user.phone,f_user.gflg,f_company.*,f_real.rflg,f_fans.*')
                ->find();
            $rss = Db::name('company')->where('uid',$uid['uid'])->field('f_company.aid')->find();
            //如果该公司的企业能力为空的则为其赋值
            if($rss['aid']=='')
            {
                $rss['aid']='暂无';
            }
            //有值的话则循环显示并且查询出每个下标所对应的能力名称
            $rs = array_filter(explode(',',$rss['aid']));
            for($i=0;$i<count($rs);$i++)
            {
                $as[] = Db::name('ability')->where('aid',$rs[$i])->field('ability')->select();
            }
            //把能力追加到总表中
            $arr['aid'] = $as;
        }else{
            $arr = Db::name('user')
                ->join('f_company','f_company.uid=f_user.uid')
                ->join('f_real','f_real.uid=f_user.uid')
                ->join('f_fans','f_fans.uid=f_user.uid')
                ->where('f_user.uid',$gid)
                ->field('f_user.nickname,f_user.avatars_img,f_user.uname,f_user.company,f_user.head_portrait,f_user.phone,f_user.gflg,f_company.*,f_real.rflg,f_fans.*')
                ->find();
            $rss = Db::name('company')->where('uid',$gid)->field('f_company.aid')->find();
            //如果该公司的企业能力为空的则为其赋值
            if($rss['aid']==''){
                $rss['aid']='暂无';
            }
            //有值的话则循环显示
            $rs = array_filter(explode(',',$rss['aid']));
            for($i=0;$i<count($rs);$i++)
            {
                $as[] = Db::name('ability')->where('aid',$rs[$i])->field('ability')->select();
            }
            $arr['aid'] = $as;
            $gua=array_filter(explode(',',$arr['fuid']));
            //判断当前登陆用户是否关注查看名片的用户
            if(in_array($uid,$gua)){
                $arr['guan']='1';
            }else{
                $arr['guan']='0';
            }
        }
        //判断用户头像是否为空为空则显示微信头像，如果两个头像都为空则为用户头像为空的添加一个临时头像（不存在数据库中的临时头像）
        if($gid!='')
        {
            if($arr['avatars_img']=='')
            {
                $rss=Db::name('user')->where('uid',$gid)->field('head_portrait')->find();
                $arr['avatars_img']=$rss['head_portrait'];
                if($arr['avatars_img']=='')
                {
                    $arr['avatars_img']='https://www.mamingwang.cn/upload/linshiimg/20190411095739.jpg';
                }
            }else{
                $arr['avatars_img']='https://www.mamingwang.cn/upload/portrait/'.$arr['avatars_img'];
            }
        }else{
            if($arr['avatars_img']=='')
            {
                $rss=Db::name('user')->where('uid',$uid['uid'])->field('head_portrait')->find();
                $arr['avatars_img']=$rss['head_portrait'];
                if($arr['avatars_img']=='')
                {
                    $arr['avatars_img']='https://www.mamingwang.cn/upload/linshiimg/20190411095739.jpg';
                }
            }else{
                $arr['avatars_img']='https://www.mamingwang.cn/upload/portrait/'.$arr['avatars_img'];
            }
        }
        //判断当前用户是否为官方账户是的话名称则显示官方账户
        if($arr['gflg']==1)
        {
            $arr['nickname']='官方账户';
        }
        //判断名称不能为空
        if($arr['rflg']==1)
        {
            $arr['nickname'] = $arr['uname'];
            if ($arr['nickname'] == '') {
                $arr['nickname'] = '';
            }
        }
        //将四套图片分割成数组
        if($arr['location_img']!='')
        {
            $rc=array_filter(explode(',',$arr['location_img']));
            $rc1=count($rc);
            if($rc1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $e[]='https://www.mamingwang.cn/upload/news/'.$rc[$i];
                    $arr['location_img']=$e;
                }
            }else{
                foreach($rc as $b)
                {
                    $box[]='https://www.mamingwang.cn/upload/news/'.$b;
                    $arr['location_img']=$box;
                }
            }
        } else {
            $arr['location_img']=[];
        }
        if($arr['place_img']!='')
        {
            $ra=array_filter(explode(',',$arr['place_img']));
            $ra1=count($ra);
            if($ra1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $d[]='https://www.mamingwang.cn/upload/news/'.$ra[$i];
                    $arr['place_img']=$d;
                }
            }else{
                foreach($ra as $b)
                {
                    $box1[]='https://www.mamingwang.cn/upload/news/'.$b;
                    $arr['place_img']=$box1;
                }
            }
        }else{
            $arr['place_img']=[];
        }
        if($arr['equipment_img']!='')
        {
            $rb=array_filter(explode(',',$arr['equipment_img']));
            $rb1=count($rb);
            if($rb1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $c[]='https://www.mamingwang.cn/upload/news/'.$rb[$i];
                    $arr['equipment_img']=$c;
                }
            }else{
                foreach($rb as $b)
                {
                    $box2[]='https://www.mamingwang.cn/upload/news/'.$b;
                    $arr['equipment_img']=$box2;
                }
            }
        }else{
            $arr['equipment_img']=[];
        }
        if($arr['license_img']!='')
        {
            $rd=array_filter(explode(',',$arr['license_img']));
            $rd1=count($rd);
            if($rd1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $a[]='https://www.mamingwang.cn/upload/news/'.$rd[$i];
                    $arr['license_img']=$a;
                }
            }else{
                foreach($rd as $b)
                {
                    $box3[]='https://www.mamingwang.cn/upload/news/'.$b;
                    $arr['license_img']=$box3;
                }
            }
        }else{
            $arr['license_img']=[];
        }
        if($arr['location_img'] ==[] && $arr['place_img'] == [] && $arr['equipment_img'] == [] && $arr['license_img'] == []){
            $arr['tupian'] = 0;
        }else{
            $arr['tupian']=[$arr['location_img'],$arr['place_img'],$arr['equipment_img'],$arr['license_img']];
        }
        //查询到粉丝总数
        $fuid=Db::name('ribiao')->where('bid',$uid['uid'])->where('zhaungtai','1')->select();
        $xfuid = Db::name('ribiao')->where('bid',$uid['uid'])->where('zhaungtai','0')->select();
        //查询到关注总数
        $guid = count(array_filter(explode(',',$arr['guid'])));
        $arr['fuid'] = count($fuid);
        $arr['xfuid'] = count($xfuid);
        $arr['guid'] = $guid;
		$arr[]=Db::name('user')->where('code',$code)->field('phone')->find();
        return $this->AjaxReturn('1',$arr);
    }
    //产品信息的显示（已测试）
    public function message()
    {
        $code=input('openid');
        $luid=Db::name('user')->where('code',$code)->field('uid')->find();
        $uid=$luid['uid'];
        $gid=input('uid');
        $time=date('Y-m-d H:i:s',time());
        if($gid!='')
        {
            $where['uid'] = $gid;
            $where['mflg'] = 1;
            $xinxi = Db::name('message')
                ->where($where)
                ->whereTime('stoptime','>',$time)
                ->order('createtime desc')
                ->select();
        }else{
            $where['uid']=$uid;
            $where['mflg']=1;
            $xinxi=Db::name('message')
                ->where($where)
                ->order('f_message.createtime desc')
                ->select();
            foreach ($xinxi as $key=>$value)
            {
                if($value['stoptime']>$time)
                {
                    $a='';
                }else{
                    $a='已过期';
                }
                $xinxi[$key]['guo']=$a;
            }
        }
        //dump($xinxi);die;
        if($xinxi==[])
        {
            $xin='';
            return $xin;
        }else{
            for($i=0;$i<count($xinxi);$i++){
                $xinxi[$i]['myuid'] = $uid;
            }
            for($i=0;$i<count($xinxi);$i++)
            {
                $rs[]=array_filter(explode(',',$xinxi[$i]['picture']));
                if($xinxi[$i]['unit']==1)
                {
                    $xinxi[$i]['unit']='吨';
                }
                elseif($xinxi[$i]['unit']==2)
                {
                    $xinxi[$i]['unit']='千克';
                }
                elseif($xinxi[$i]['unit']==3)
                {
                    $xinxi[$i]['unit']='斤';
                }elseif($xinxi[$i]['unit']==4)
                {
                    $xinxi[$i]['unit']='个';
                }
            }
            //只输出每组图片的第一个字符串并为其加上地址
            for($i=0;$i<count($rs);$i++)
            {
                if($rs[$i])
                {
                    $xinxi[$i]['picture']='https://www.mamingwang.cn/upload/message/'.$rs[$i][0];
                }else{
                    $xinxi[$i]['picture']='https://www.mamingwang.cn/upload/message/no.jpg';
                }
            }
            return $this->AjaxReturn('1','操作成功',$xinxi);
        }
    }
    //产品信息的删除(测试)
    public function del()
    {
        $code=input('openid');
        $luid=Db::name('user')->where('code',$code)->field('uid')->find();
        $uid=$luid['uid'];
        $mid=input('mid');
        $del=Db::name('message')
            ->where('f_message.mid',$mid)
            ->update([
                'mflg'=>0
            ]);
        if($del){
            return $this->AjaxReturn('1','删除成功');
        }else{
            return $this->AjaxReturn('-1','信息错误');
        }
    }
 
   public function InformationList(){
     	$code = input('openid');
     	$phone=Db::name('user')->where('code',$code)->field('phone')->find();
      	$page=input('page');
        $page=($page-1)*6;
            $sql=db('zixun')
                ->where('flg',0)
              	->order('createtime desc')
                ->limit($page,6)
                ->select();
            foreach ($sql as $key=>$value){
                $value['prctures']="https://www.mamingwang.cn" .$value['picture'];
              $value['phone']=$phone['phone'];
                $arr[]=$value;
            }
            if (!empty($sql)){
                return $this->AjaxReturn("-1","该用户未实名认证",$arr);
            }else{
                return $this->AjaxReturn("-2","操作失败");
            }
    }

}